home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 July / CMCD0703.ISO / Software / Test / audacity / audacity-win-1_1_3.exe / {app} / Nyquist / nyquist.lsp < prev    next >
Encoding:
Lisp/Scheme  |  2002-09-16  |  49.1 KB  |  1,523 lines

  1. ;;;
  2.  
  3. ;;;   ###########################################################
  4.  
  5. ;;;   ### NYQUIST-- A Language for Composition and Synthesis. ###
  6.  
  7. ;;;   ###                                                     ###
  8.  
  9. ;;;   ### Copyright (c) 1994 by Roger B. Dannenberg           ###
  10.  
  11. ;;;   ###########################################################
  12.  
  13. ;;;
  14.  
  15.  
  16.  
  17. ;;;   
  18.  
  19. ;;;   Modifications for using Nyquist within Audacity
  20.  
  21. ;;;   by Dominic Mazzoni
  22.  
  23. ;;;
  24.  
  25.  
  26.  
  27. (prog ()
  28.  
  29.    (setq lppp -12.0) (setq lpp -9.0)  (setq lp -6.0)    (setq lmp -3.0)
  30.  
  31.    (setq lfff 12.0) (setq lff 9.0)  (setq lf 6.0)    (setq lmf 3.0)
  32.  
  33.    (setq dB0 1.00)  (setq dB1 1.122) (setq dB10 3.1623)
  34.  
  35.  
  36.  
  37.    (setq s 0.25) (setq sd 0.375) (setq st (/ 0.5 3.0))
  38.  
  39.    (setq i 0.5)  (setq id 0.75)  (setq it (* st 2.0))
  40.  
  41.    (setq q 1.0)  (setq qd 1.5)   (setq qt (* st 4.0))
  42.  
  43.    (setq h 2.0)  (setq hd 3.0)   (setq ht (* st 8.0))
  44.  
  45.    (setq w 4.0)  (setq wd 6.0)   (setq wt (* st 16.0))
  46.  
  47. )
  48.  
  49.  
  50.  
  51. (if (not (boundp '*A4-Hertz*))
  52.  
  53.     (setf *A4-Hertz* 440.0))
  54.  
  55.  
  56.  
  57. ; next pitch, for initializations below
  58.  
  59.  
  60. (defun np () (incf nyq:next-pitch))
  61.  
  62.  
  63.  
  64. (defun set-pitch-names ()
  65.  
  66.    (setq no-pitch 116.0)
  67.  
  68.    ; note: 58.0 is A4 - (C0 - 1) = 69 - (12 - 1)
  69.  
  70.    (setf nyq:next-pitch (- (hz-to-step *A4-Hertz*) 58.0))
  71.  
  72.  
  73.  
  74.    (setf nyq:pitch-names
  75.  
  76.     '(c0 (cs0 df0) d0 (ds0 ef0) e0 f0 (fs0 gf0) g0 (gs0 af0) a0
  77.  
  78.       (as0 bf0) b0
  79.  
  80.       c1 (cs1 df1) d1 (ds1 ef1) e1 f1 (fs1 gf1) g1 (gs1 af1) a1
  81.  
  82.       (as1 bf1) b1
  83.  
  84.       c2 (cs2 df2) d2 (ds2 ef2) e2 f2 (fs2 gf2) g2 (gs2 af2) a2
  85.  
  86.       (as2 bf2) b2
  87.  
  88.       c3 (cs3 df3) d3 (ds3 ef3) e3 f3 (fs3 gf3) g3 (gs3 af3) a3
  89.  
  90.       (as3 bf3) b3
  91.  
  92.       c4 (cs4 df4) d4 (ds4 ef4) e4 f4 (fs4 gf4) g4 (gs4 af4) a4
  93.  
  94.       (as4 bf4) b4
  95.  
  96.       c5 (cs5 df5) d5 (ds5 ef5) e5 f5 (fs5 gf5) g5 (gs5 af5) a5
  97.  
  98.       (as5 bf5) b5
  99.  
  100.       c6 (cs6 df6) d6 (ds6 ef6) e6 f6 (fs6 gf6) g6 (gs6 af6) a6
  101.  
  102.       (as6 bf6) b6
  103.  
  104.       c7 (cs7 df7) d7 (ds7 ef7) e7 f7 (fs7 gf7) g7 (gs7 af7) a7
  105.  
  106.       (as7 bf7) b7))
  107.  
  108.  
  109.  
  110.    (dolist (p nyq:pitch-names)
  111.  
  112.      (cond ((atom p) (set p (np)))
  113.  
  114.        (t (let ((pitch (np)))
  115.  
  116.         (dolist (s p) (set s pitch)))))))
  117.  
  118.  
  119.  
  120.  
  121.  
  122. (set-pitch-names)
  123.  
  124.  
  125.  
  126. (if (not (boundp '*DEFAULT-SOUND-SRATE*))
  127.  
  128.   (setf *DEFAULT-SOUND-SRATE* 44100.0))
  129.  
  130. (if (not (boundp '*DEFAULT-CONTROL-SRATE*))
  131.  
  132.   (setf *DEFAULT-CONTROL-SRATE* 2205.0))
  133.  
  134.  
  135.  
  136. (setf *environment-variables*
  137.  
  138.       '(*WARP* *SUSTAIN* *START* *LOUD* *TRANSPOSE* 
  139.  
  140.     *STOP* *CONTROL-SRATE* *SOUND-SRATE*))
  141.  
  142.  
  143.  
  144. (setfn environment-time car)
  145.  
  146. (setfn environment-stretch cadr)
  147.  
  148.  
  149.  
  150. ; ENVIRONMENT-MAP - map virtual time using an environment
  151.  
  152. ;
  153.  
  154. ;(defun environment-map (env tim)
  155.  
  156. ;  (+ (environment-time env)
  157.  
  158. ;     (* (environment-stretch env) tim)))
  159.  
  160.  
  161.  
  162.  
  163.  
  164. (defun nyq:the-environment () (mapcar 'eval *environment-variables*))
  165.  
  166.  
  167.  
  168.  
  169.  
  170. ;; GLOBAL ENVIRONMENT VARIABLES and their startup values:
  171.  
  172. (defun nyq:environment-init ()
  173.  
  174.   (setq *WARP*        '(0.0 1.0 nil))
  175.  
  176.   (setq *LOUD*    0.0)   ; now in dB
  177.  
  178.   (setq *TRANSPOSE*    0.0)
  179.  
  180.   (setq *SUSTAIN*            1.0)
  181.  
  182.   (setq *START*       MIN-START-TIME)
  183.  
  184.   (setq *STOP*        MAX-STOP-TIME)
  185.  
  186.   (setq *CONTROL-SRATE*  *DEFAULT-CONTROL-SRATE*)
  187.  
  188.   (setq *SOUND-SRATE* *DEFAULT-SOUND-SRATE*)
  189.  
  190.   t)                ; return nothing in particular
  191.  
  192.  
  193.  
  194. (nyq:environment-init)
  195.  
  196.  
  197.  
  198. (defun get-duration (dur)
  199.  
  200.   (- (local-to-global (* (get-sustain) dur))
  201.  
  202.      (setf *rslt* (local-to-global 0))))
  203.  
  204.  
  205.  
  206.  
  207.  
  208. (defun get-loud ()
  209.  
  210.   (cond ((numberp *loud*) *loud*)
  211.  
  212.     ((soundp *loud*)
  213.  
  214.      (sref *loud* 0))
  215.  
  216.     (t
  217.  
  218.      (error (format t "*LOUD* should be a number or sound: ~A" *LOUD*)))))
  219.  
  220.  
  221.  
  222.  
  223.  
  224. (defun get-sustain ()
  225.  
  226.   (cond ((numberp *SUSTAIN*) *SUSTAIN*)
  227.  
  228.     ((soundp *SUSTAIN*)
  229.  
  230.      ;(display "get-sustain: lookup " (local-to-global 0) 0))
  231.  
  232.      (sref *SUSTAIN* 0))
  233.  
  234.     (t
  235.  
  236.      (error (format t "*SUSTAIN* should be a number or sound: ~A" *SUSTAIN*)))))
  237.  
  238.  
  239.  
  240.  
  241.  
  242. (defun get-tempo ()
  243.  
  244.   (slope (snd-inverse (get-warp) (local-to-global 0)
  245.  
  246.               *control-srate*)))
  247.  
  248.  
  249.  
  250. (defun get-transpose ()
  251.  
  252.   (cond ((numberp *TRANSPOSE*) *TRANSPOSE*)
  253.  
  254.     ((soundp *TRANSPOSE*)
  255.  
  256.      ; (display "get-transpose: lookup " 0)
  257.  
  258.      ; (format t "samples: ~A~%" (snd-samples *TRANSPOSE* 100))
  259.  
  260.      (sref *TRANSPOSE* 0))
  261.  
  262.     (t
  263.  
  264.      (error (format t "*TRANSPOSE* should be a number or sound: ~A" *TRANSPOSE*)))))
  265.  
  266.  
  267.  
  268.  
  269.  
  270. (defun get-warp ()
  271.  
  272.   (let ((f (warp-function *WARP*)))
  273.  
  274.     (cond ((null f) (error "Null warp function"))
  275.  
  276.     (t
  277.  
  278.      (shift-time (scale-srate f (/ (warp-stretch *WARP*)))
  279.  
  280.              (- (warp-time *WARP*)))))))
  281.  
  282.  
  283.  
  284.  
  285.  
  286. ;;;;;;;;;;;;;;;;;;;;;;
  287.  
  288. ;; OSCILATORS
  289.  
  290. ;;;;;;;;;;;;;;;;;;;;;;
  291.  
  292.  
  293.  
  294. (defun build-harmonic (n table-size) (snd-sine 0 n table-size 1))
  295.  
  296.  
  297.  
  298. (setf *SINE-TABLE* (list (build-harmonic 1 2048)
  299.  
  300.              (hz-to-step 1.0)
  301.  
  302.              T))
  303.  
  304. (setf *TABLE* *SINE-TABLE*)
  305.  
  306.  
  307.  
  308.  
  309.  
  310. ;; AMOSC
  311.  
  312. ;;
  313.  
  314. (defun amosc (pitch modulation &optional (sound *table*) (phase 0.0))
  315.  
  316.   (let ((modulation-srate (snd-srate modulation))
  317.  
  318.     (hz (step-to-hz (+ pitch (get-transpose)))))
  319.  
  320.     (cond ((> *SOUND-SRATE* modulation-srate)
  321.  
  322.        (setf modulation (snd-up *SOUND-SRATE* modulation)))
  323.  
  324.       ((< *SOUND-SRATE* modulation-srate)
  325.  
  326.        (format t "Warning: down-sampling AM modulation in amosc~%")
  327.  
  328.        (setf modulation (snd-down *SOUND-SRATE* modulation))))
  329.  
  330.     (cond ((> hz (/ *SOUND-SRATE* 2))
  331.  
  332.        (format t "Warning: amosc frequency (~A hz) will alias at current sample rate (~A hz).\n"
  333.  
  334.            hz *SOUND-SRATE*)))
  335.  
  336.     (scale-db (get-loud)
  337.  
  338.       (snd-amosc
  339.  
  340.     (car sound)    ; samples for table
  341.  
  342.     (cadr sound)    ; step represented by table
  343.  
  344.     *SOUND-SRATE*    ; output sample rate
  345.  
  346.     hz        ;  output hz
  347.  
  348.     (local-to-global 0)    ; starting time
  349.  
  350.     modulation    ; modulation
  351.  
  352.     phase))))    ; phase
  353.  
  354.  
  355.  
  356.  
  357.  
  358. ;; FMOSC
  359.  
  360. ;;
  361.  
  362. ;; modulation rate must be less than or equal to sound-srate, so
  363.  
  364. ;; force resampling and issue a warning if necessary. snd-fmosc can
  365.  
  366. ;; handle upsampling cases internally.
  367.  
  368. ;;
  369.  
  370. (defun fmosc (pitch modulation &optional (sound *table*) (phase 0.0))
  371.  
  372.   (let ((modulation-srate (snd-srate modulation))
  373.  
  374.         (hz (step-to-hz (+ pitch (get-transpose)))))
  375.  
  376.     (cond ((< *SOUND-SRATE* modulation-srate)
  377.  
  378.        (format t "Warning: down-sampling FM modulation in fmosc~%")
  379.  
  380.        (setf modulation (snd-down *SOUND-SRATE* modulation))))
  381.  
  382.     (cond ((> hz (/ *SOUND-SRATE* 2))
  383.  
  384.        (format t "Warning: fmosc nominal frequency (~A hz) will alias at current sample rate (~A hz).\n"
  385.  
  386.            hz *SOUND-SRATE*)))
  387.  
  388.     (scale-db (get-loud)
  389.  
  390.       (snd-fmosc 
  391.  
  392.         (car sound)        ; samples for table
  393.  
  394.         (cadr sound)        ; step represented by table
  395.  
  396.         *SOUND-SRATE*        ; output sample rate
  397.  
  398.         hz            ;  output hz
  399.  
  400.         (local-to-global 0)    ; starting time
  401.  
  402.         modulation        ; modulation
  403.  
  404.         phase))))        ; phase
  405.  
  406.  
  407.  
  408.  
  409.  
  410. ;; BUZZ
  411.  
  412. ;;
  413.  
  414. ;; (ARGUMENTS ("long" "n") ("rate_type" "sr") ("double" "hz")
  415.  
  416. ;;            ("time_type" "t0") ("sound_type" "s_fm"))
  417.  
  418. ;; 
  419.  
  420. (defun buzz (n pitch modulation)
  421.  
  422.   (let ((modulation-srate (snd-srate modulation))
  423.  
  424.         (hz (step-to-hz (+ pitch (get-transpose)))))
  425.  
  426.     (cond ((< *SOUND-SRATE* modulation-srate)
  427.  
  428.            (format t "Warning: down-sampling modulation in buzz~%")
  429.  
  430.            (setf modulation (snd-down *SOUND-SRATE* modulation))))
  431.  
  432.     (cond ((> hz (/ *SOUND-SRATE* 2))
  433.  
  434.            (format t "Warning: buzz nominal frequency (~A hz) will alias at current sample rate (~A hz).\n"
  435.  
  436.                    hz *SOUND-SRATE*)))
  437.  
  438.     (setf n (min n 1)) ; avoid divide by zero problem
  439.  
  440.     (scale-db (get-loud)
  441.  
  442.               (snd-buzz n                   ; number of harmonics
  443.  
  444.                         *SOUND-SRATE*       ; output sample rate
  445.  
  446.                         hz                  ; output hz
  447.  
  448.                         (local-to-global 0) ; starting time
  449.  
  450.                         modulation))))      ; freq. modulation
  451.  
  452.                         
  453.  
  454.  
  455.  
  456. ;; (HZOSC hz [table [phase]])
  457.  
  458. ;;
  459.  
  460. ;; similar to FMOSC, but without "carrier" frequency parameter
  461.  
  462. ;; also, hz may be a scalar or a sound
  463.  
  464. ;;
  465.  
  466. (defun hzosc (hz &optional (sound *table*) (phase 0.0))
  467.  
  468.   (let (hz-srate)
  469.  
  470.     (cond ((numberp hz)
  471.  
  472.            (osc (hz-to-step hz) 1.0 sound phase))
  473.  
  474.           (t
  475.  
  476.            (setf hz-srate (snd-srate hz))
  477.  
  478.            (cond ((< *SOUND-SRATE* hz-srate)
  479.  
  480.                   (format t "Warning: down-sampling hz in hzosc~%")
  481.  
  482.                   (setf hz (snd-down *SOUND-SRATE* hz))))
  483.  
  484.            (scale-db (get-loud)
  485.  
  486.                      (snd-fmosc (car sound) ; samples for table
  487.  
  488.                                 (cadr sound) ; step repr. by table
  489.  
  490.                                 *SOUND-SRATE* ; output sample rate
  491.  
  492.                                 0.0 ; dummy carrier
  493.  
  494.                                 (local-to-global 0) ; starting time
  495.  
  496.                                 hz phase))))))
  497.  
  498.  
  499.  
  500.  
  501.  
  502. ;; (SIOSC-BREAKPOINTS tab0 t1 tab1 ... tn tabn)
  503.  
  504. ;;   converts times to sample numbers
  505.  
  506. ;; NOTE: time-warping the spectral envelope seems
  507.  
  508. ;; like the wrong thing to do (wouldn't it be better
  509.  
  510. ;; to warp the parameters that control the spectra,
  511.  
  512. ;; or don't warp at all?). Nominally, a note should
  513.  
  514. ;; have a "score" or local time duration equal to the
  515.  
  516. ;; SUSTAIN environment variable. (When sustain is 1.0
  517.  
  518. ;; and no time-warping is in effect, the duration is 1).
  519.  
  520. ;; So, scale all times by
  521.  
  522. ;;        (local-to-global (get-sustain))
  523.  
  524. ;; so that if the final time tn = 1.0, we get a nominal
  525.  
  526. ;; length note.
  527.  
  528.  
  529.  
  530. (defun siosc-breakpoints (breakpoints)
  531.  
  532.   (display "siosc-breakpoints" breakpoints)
  533.  
  534.   (prog (sample-count result (last-count 0) time-factor)
  535.  
  536.     (setf time-factor
  537.  
  538.       (- (local-to-global (get-sustain))
  539.  
  540.          (local-to-global 0.0)))
  541.  
  542.     (setf time-factor (* time-factor *SOUND-SRATE*))
  543.  
  544.     (cond ((and (listp breakpoints)
  545.  
  546.         (cdr breakpoints)
  547.  
  548.         (cddr breakpoints)))
  549.  
  550.       (t (error "SIOSC table list must have at least 3 elements")))
  551.  
  552. loop
  553.  
  554.     (cond ((and (listp breakpoints)
  555.  
  556.            (soundp (car breakpoints)))
  557.  
  558.        (push (car breakpoints) result)
  559.  
  560.        (setf breakpoints (cdr breakpoints)))
  561.  
  562.       (t
  563.  
  564.        (error "SIOSC expecting SOUND in table list")))
  565.  
  566.     (cond ((and breakpoints
  567.  
  568.         (listp breakpoints)
  569.  
  570.         (numberp (car breakpoints)))
  571.  
  572.        (setf sample-count (truncate
  573.  
  574.         (+ 0.5 (* time-factor (car breakpoints)))))
  575.  
  576.        (cond ((< sample-count last-count)
  577.  
  578.           (setf sample-count (1+ last-count))))
  579.  
  580.        (push sample-count result)
  581.  
  582.        (setf last-count sample-count)
  583.  
  584.        (setf breakpoints (cdr breakpoints))
  585.  
  586.        (cond (breakpoints
  587.  
  588.           (go loop))))
  589.  
  590.       (breakpoints
  591.  
  592.        (error "SIOSC expecting number (time) in table list")))
  593.  
  594.     (setf result (reverse result))
  595.  
  596.     (display "siosc-breakpoints" result)
  597.  
  598.     (return result)))
  599.  
  600.  
  601.  
  602. ;; SIOSC -- spectral interpolation oscillator
  603.  
  604. ;;
  605.  
  606. ;; modulation rate must be less than or equal to sound-srate, so
  607.  
  608. ;; force resampling and issue a warning if necessary. snd-fmosc can
  609.  
  610. ;; handle upsampling cases internally.
  611.  
  612. ;;
  613.  
  614. (defun siosc (pitch modulation breakpoints)
  615.  
  616.   (let ((modulation-srate (snd-srate modulation))
  617.  
  618.     (hz (step-to-hz (+ pitch (get-transpose)))))
  619.  
  620.     (cond ((< *SOUND-SRATE* modulation-srate)
  621.  
  622.        (format t "Warning: down-sampling FM modulation in siosc~%")
  623.  
  624.        (setf modulation (snd-down *SOUND-SRATE* modulation))))
  625.  
  626.     (cond ((> hz (/ *SOUND-SRATE* 2))
  627.  
  628.        (format t "Warning: siosc nominal frequency (~A hz) will alias at current sample rate (~A hz).\n"
  629.  
  630.            hz *SOUND-SRATE*)))
  631.  
  632.      (scale-db (get-loud)
  633.  
  634.       (snd-siosc 
  635.  
  636.     (siosc-breakpoints breakpoints)    ; tables
  637.  
  638.     *SOUND-SRATE*        ; output sample rate
  639.  
  640.     hz            ;  output hz
  641.  
  642.     (local-to-global 0)    ; starting time
  643.  
  644.     modulation))))        ; modulation
  645.  
  646.  
  647.  
  648.  
  649.  
  650. ;; LFO -- freq &optional duration sound phase)
  651.  
  652. ;;
  653.  
  654. ;; Default duration is 1.0 sec, default sound is *TABLE*, 
  655.  
  656. ;; default phase is 0.0.
  657.  
  658. ;;
  659.  
  660. (defun lfo (freq &optional (duration 1.0)
  661.  
  662.          (sound *SINE-TABLE*) (phase 0.0))
  663.  
  664.   (let ((d (get-duration duration)))
  665.  
  666.     (if (minusp d) (setf d 0))
  667.  
  668.     (cond ((> freq (/ *CONTROL-SRATE* 2))
  669.  
  670.        (format t "Warning: lfo frequency (~A hz) will alias at current control rate (~A hz).\n"
  671.  
  672.            freq *CONTROL-SRATE*)))
  673.  
  674.      (snd-osc
  675.  
  676.     (car sound)        ; samples for table
  677.  
  678.     (cadr sound)        ; step represented by table
  679.  
  680.     *CONTROL-SRATE*        ; output sample rate
  681.  
  682.     freq            ; output hz
  683.  
  684.     *rslt*            ; starting time
  685.  
  686.     d            ; duration
  687.  
  688.     phase)))        ; phase
  689.  
  690.  
  691.  
  692.  
  693.  
  694. ;; OSC - table lookup oscillator
  695.  
  696. ;;
  697.  
  698. (defun osc (pitch &optional (duration 1.0) 
  699.  
  700.           (sound *TABLE*) (phase 0.0))
  701.  
  702.   (let ((d (get-duration duration))
  703.  
  704.     (hz (step-to-hz (+ pitch (get-transpose)))))
  705.  
  706.     ;(display "osc" *warp* global-start global-stop actual-dur  
  707.  
  708.     ;         (get-transpose))
  709.  
  710.     (cond ((> hz (/ *SOUND-SRATE* 2))
  711.  
  712.        (format t "Warning: osc frequency (~A hz) will alias at current sample rate (~A hz).\n"
  713.  
  714.            hz *SOUND-SRATE*)))
  715.  
  716.      (set-logical-stop
  717.  
  718.      (scale-db (get-loud)
  719.  
  720.            (snd-osc 
  721.  
  722.         (car sound)        ; samples for table
  723.  
  724.         (cadr sound)        ; step represented by table
  725.  
  726.         *SOUND-SRATE*        ; output sample rate
  727.  
  728.         hz            ;  output hz
  729.  
  730.         *rslt*            ; starting time
  731.  
  732.         d            ; duration
  733.  
  734.         phase))                 ; phase
  735.  
  736.      duration)))
  737.  
  738.  
  739.  
  740.  
  741.  
  742. ;; PARTIAL -- sine osc with built-in envelope scaling
  743.  
  744. ;;
  745.  
  746. (defun partial (steps env)
  747.  
  748.   (let ((hz (step-to-hz (+ steps (get-transpose)))))
  749.  
  750.     (cond ((> hz (/ *SOUND-SRATE* 2))
  751.  
  752.        (format t "Warning: partial frequency (~A hz) will alias at current sample rate (~A hz).\n"
  753.  
  754.            hz *SOUND-SRATE*)))
  755.  
  756.     (snd-partial *sound-srate*
  757.  
  758.          hz
  759.  
  760.          env)))
  761.  
  762.  
  763.  
  764.  
  765.  
  766. ;; SAMPLER -- simple attack + sustain sampler
  767.  
  768. ;;
  769.  
  770. (defun sampler (pitch modulation 
  771.  
  772.         &optional (sample *table*) (npoints 2))
  773.  
  774.   (let ((samp (car sample))
  775.  
  776.     (samp-pitch (cadr sample))
  777.  
  778.     (samp-loop-start (caddr sample))
  779.  
  780.     (hz (step-to-hz (+ pitch (get-transpose)))))
  781.  
  782.     ; make a waveform table look like a sample with no attack:
  783.  
  784.     (cond ((not (numberp samp-loop-start))
  785.  
  786.        (setf samp-loop-start 0.0)))
  787.  
  788.     (cond ((> hz (/ *SOUND-SRATE* 2))
  789.  
  790.        (format t "Warning: sampler nominal frequency (~A hz) will alias at current sample rate (~A hz).\n"
  791.  
  792.            hz *SOUND-SRATE*)))
  793.  
  794.     (scale-db (get-loud)
  795.  
  796.        (snd-sampler 
  797.  
  798.     samp        ; samples for table
  799.  
  800.     samp-pitch        ; step represented by table
  801.  
  802.     samp-loop-start         ; time to start loop
  803.  
  804.     *SOUND-SRATE*        ; output sample rate
  805.  
  806.     hz            ;  output hz
  807.  
  808.     (local-to-global 0)    ; starting time
  809.  
  810.     modulation        ; modulation
  811.  
  812.     npoints))))        ; number of interpolation points
  813.  
  814.  
  815.  
  816.  
  817.  
  818. ;; SINE -- simple sine oscillator
  819.  
  820. ;;
  821.  
  822. (defun sine (steps &optional (duration 1.0))
  823.  
  824.   (let ((hz (step-to-hz (+ steps (get-transpose))))
  825.  
  826.     (d (get-duration duration)))
  827.  
  828.     (cond ((> hz (/ *SOUND-SRATE* 2))
  829.  
  830.        (format t "Warning: sine frequency (~A hz) will alias at current sample rate (~A hz).\n"
  831.  
  832.            hz *SOUND-SRATE*)))
  833.  
  834.     (snd-sine *rslt* hz *sound-srate* d)))
  835.  
  836.  
  837.  
  838.  
  839.  
  840. ;; PLUCK
  841.  
  842. ;;
  843.  
  844. ;; (ARGUMENTS ("double" "sr") ("double" "hz") ("time_type" "t0") 
  845.  
  846. ;;            ("time_type" "d") ("double" "final_amp"))
  847.  
  848. ;;
  849.  
  850. (defun pluck (steps &optional (duration 1.0) (final-amp 0.001))
  851.  
  852.   (let ((hz (step-to-hz (+ steps (get-transpose))))
  853.  
  854.         (d (get-duration duration)))
  855.  
  856.     (cond ((> hz (/ *SOUND-SRATE* 2))
  857.  
  858.        (format t "Warning: pluck frequency (~A hz) will alias at current sample rate (~A hz).\n"
  859.  
  860.            hz *SOUND-SRATE*)))
  861.  
  862.     (snd-pluck *SOUND-SRATE* hz *rslt* d final-amp)))
  863.  
  864.  
  865.  
  866.  
  867.  
  868.  
  869.  
  870.  
  871.  
  872. ;; abs-env -- restore the standard environment
  873.  
  874. ;;
  875.  
  876. (defmacro abs-env (s)
  877.  
  878.   `(progv '(*WARP* *LOUD* *TRANSPOSE* *SUSTAIN* 
  879.  
  880.         *START* *STOP* *CONTROL-SRATE* *SOUND-SRATE*)
  881.  
  882.       (list '(0.0 1.0 NIL) 0.0 0.0 1.0
  883.  
  884.         -1e+9 1e+9 *DEFAULT-CONTROL-SRATE* *DEFAULT-SOUND-SRATE*)
  885.  
  886.       ,s))
  887.  
  888.  
  889.  
  890.  
  891.  
  892. ; nyq:add2 - add two arguments
  893.  
  894.  
  895. (defun nyq:add2 (s1 s2)
  896.  
  897.     (cond ((and (arrayp s1) (not (arrayp s2)))
  898.  
  899.        (setf s2 (vector s2)))
  900.  
  901.       ((and (arrayp s2) (not (arrayp s1)))
  902.  
  903.        (setf s1 (vector s1))))
  904.  
  905.     (cond ((arrayp s1)
  906.  
  907.        (sum-of-arrays s1 s2))
  908.  
  909.       (t
  910.  
  911.        (nyq:add-2-sounds s1 s2))))
  912.  
  913.  
  914.  
  915.  
  916.  
  917. ; (NYQ:ADD-2-SOUNDS S1 S2) - add two sound (or number) arguments
  918.  
  919.  
  920. (defun nyq:add-2-sounds (s1 s2)
  921.  
  922.   (cond ((numberp s1)
  923.  
  924.      (cond ((numberp s2)
  925.  
  926.         (+ s1 s2))
  927.  
  928.            (t
  929.  
  930.         (snd-offset s2 s1))))
  931.  
  932.     ((numberp s2)
  933.  
  934.      (snd-offset s1 s2))
  935.  
  936.     (t
  937.  
  938.      (let ((s1sr (snd-srate s1))
  939.  
  940.            (s2sr (snd-srate s2)))
  941.  
  942. ;    (display "nyq:add-2-sounds" s1sr s2sr)
  943.  
  944.        (cond ((> s1sr s2sr)
  945.  
  946.           (snd-add s1 (snd-up s1sr s2)))
  947.  
  948.          ((< s1sr s2sr)
  949.  
  950.           (snd-add (snd-up s2sr s1) s2))
  951.  
  952.          (t
  953.  
  954.           (snd-add s1 s2)))))))
  955.  
  956.  
  957.  
  958.  
  959.  
  960.  
  961.  
  962.  
  963.  
  964. (defmacro at (x s)
  965.  
  966.  `(progv '(*WARP*) (list (list (+ (warp-time *WARP*) 
  967.  
  968.                   (* (warp-stretch *WARP*) ,x))
  969.  
  970.                    (warp-stretch *WARP*)
  971.  
  972.                    (warp-function *WARP*)))
  973.  
  974.       ,s))
  975.  
  976.  
  977.  
  978.  
  979.  
  980. ;; (AT-ABS t behavior) evaluate behavior at global time t
  981.  
  982. ;;
  983.  
  984. ;; *WARP* is the triple (d s f) denoting the function f(st+d),
  985.  
  986. ;; a mapping from local to global time.
  987.  
  988. ;; We want (d' s f) such that f(s*0 + d') = t
  989.  
  990. ;; (Note that we keep the same s and f, and only change the offset.
  991.  
  992. ;; To eliminate the warp and stretch use "(abs-env (at t behavior))")
  993.  
  994. ;; Applying the inverse of f, d' = f-1(t), or (sref (snd-inverse f ...) t)
  995.  
  996. ;; Rather than invert the entire function just to evaluate at one point,
  997.  
  998. ;; we use SREF-INVERSE to find d'.
  999.  
  1000. ;;
  1001.  
  1002. (defmacro at-abs (x s)
  1003.  
  1004.  `(progv '(*WARP*)
  1005.  
  1006.      (if (warp-function *WARP*)
  1007.  
  1008.                (list (list (sref-inverse (warp-function *WARP*) ,x)
  1009.  
  1010.                    (warp-stretch *WARP*)
  1011.  
  1012.                    (warp-function *WARP*)))
  1013.  
  1014.                (list (list ,x (warp-stretch *WARP*) NIL)))
  1015.  
  1016.      ,s))
  1017.  
  1018.  
  1019.  
  1020. ;; (CLIP S1 VALUE) - clip maximum amplitude to value
  1021.  
  1022. ;
  1023.  
  1024. (defun clip (x v)
  1025.  
  1026.   (cond ((numberp x)
  1027.  
  1028.      (max (min x v) (- v)))
  1029.  
  1030.     ((arrayp x)
  1031.  
  1032.      (let* ((len (length x))
  1033.  
  1034.         (result (make-array len)))
  1035.  
  1036.         (dotimes (i len)
  1037.  
  1038.         (setf (aref result i) 
  1039.  
  1040.               (snd-clip (aref x i) v)))
  1041.  
  1042.         result))
  1043.  
  1044.     (t
  1045.  
  1046.      (snd-clip x v))))
  1047.  
  1048.  
  1049.  
  1050. ;; (NYQ:COERCE-TO S1 S2) - expand sound s1 to type of s2
  1051.  
  1052.  
  1053. (defun nyq:coerce-to (s1 s2)
  1054.  
  1055.   (cond ((soundp s1)
  1056.  
  1057.      (cond ((arrayp s2)
  1058.  
  1059.         (nyq:sound-to-array s1 (length s2)))
  1060.  
  1061.            (t s1)))
  1062.  
  1063.     (t s1)))
  1064.  
  1065.  
  1066.  
  1067.  
  1068.  
  1069. (defmacro continuous-control-warp (beh)
  1070.  
  1071.   `(snd-compose (warp-abs nil ,beh)
  1072.  
  1073.         (snd-inverse (get-warp)
  1074.  
  1075.          (local-to-global 0) *control-srate*)))
  1076.  
  1077.  
  1078.  
  1079. (defmacro continuous-sound-warp (beh)
  1080.  
  1081.   `(snd-compose (warp-abs nil ,beh)
  1082.  
  1083.         (snd-inverse (get-warp)
  1084.  
  1085.          (local-to-global 0) *sound-srate*)))
  1086.  
  1087.  
  1088.  
  1089.  
  1090.  
  1091. (defmacro control-srate-abs (r s)
  1092.  
  1093.   `(progv '(*CONTROL-SRATE*) (list ,r)
  1094.  
  1095.       ,s))
  1096.  
  1097.  
  1098.  
  1099. ; db = 20log(ratio)
  1100.  
  1101. ; db = 20 ln(ratio)/ln(10)
  1102.  
  1103. ; db/20 = ln(ratio)/ln(10)
  1104.  
  1105. ; db ln(10)/20 = ln(ratio)
  1106.  
  1107. ; e^(db ln(10)/20) = ratio
  1108.  
  1109. ;
  1110.  
  1111. (setf ln10over20 (/ (log 10.0) 20))
  1112.  
  1113.  
  1114.  
  1115. (defun db-to-linear (x) 
  1116.  
  1117.   (cond ((numberp x)
  1118.  
  1119.      (exp (* ln10over20 x)))
  1120.  
  1121.     ((arrayp x)
  1122.  
  1123.      (let* ((len (length x))
  1124.  
  1125.         (result (make-array len)))
  1126.  
  1127.         (dotimes (i len)
  1128.  
  1129.         (setf (aref result i) 
  1130.  
  1131.               (snd-exp (snd-scale ln10over20 (aref snd i)))))
  1132.  
  1133.         result))
  1134.  
  1135.     (t
  1136.  
  1137.      (snd-exp (snd-scale ln10over20 x)))))
  1138.  
  1139.  
  1140.  
  1141.  
  1142.  
  1143. (defun linear-to-db (x) 
  1144.  
  1145.   (cond ((numberp x)
  1146.  
  1147.      (/ (log (float x)) ln10over20))
  1148.  
  1149.     ((arrayp x)
  1150.  
  1151.      (let* ((len (length x))
  1152.  
  1153.         (result (make-array len)))
  1154.  
  1155.         (dotimes (i len)
  1156.  
  1157.         (setf (aref result i) 
  1158.  
  1159.               (snd-scale (/ 1.0 ln10over20) (snd-log (aref snd i)))))
  1160.  
  1161.         result))
  1162.  
  1163.     (t
  1164.  
  1165.      (snd-scale (/ 1.0 ln10over20) (snd-log x)))))
  1166.  
  1167.  
  1168.  
  1169. ; sref - access a sound at a given time point
  1170.  
  1171. ;    note that the time is transformed to global
  1172.  
  1173. (defun sref (sound point)
  1174.  
  1175.   (snd-sref sound (local-to-global point)))
  1176.  
  1177.  
  1178.  
  1179.  
  1180.  
  1181. ; extract - start is stretched and shifted as is stop
  1182.  
  1183. ;  result is shifted to start at local time zero
  1184.  
  1185. (defun extract (start stop sound)
  1186.  
  1187.   (snd-xform sound (snd-srate sound) (local-to-global 0) 
  1188.  
  1189.          (local-to-global start) (local-to-global stop) 1.0))
  1190.  
  1191.  
  1192.  
  1193. (defun extract-abs (start stop sound)
  1194.  
  1195.   (snd-xform sound (snd-srate sound) 0 start stop 1.0))
  1196.  
  1197.      
  1198.  
  1199.  
  1200.  
  1201. ;(defmacro extract (start stop sound)
  1202.  
  1203. ;  `(let ($newsound)
  1204.  
  1205. ;     (progv '(*START* *STOP*)
  1206.  
  1207. ;            (list (local-to-global ,start)
  1208.  
  1209. ;          (local-to-global ,stop))
  1210.  
  1211. ;            (setf $newsound ,sound)
  1212.  
  1213. ;            (setf $newsound 
  1214.  
  1215. ;                  (loud-abs 0 (cue (set-logical-stop-abs $newsound *STOP*)))))
  1216.  
  1217. ;     $newsound))
  1218.  
  1219.  
  1220.  
  1221.  
  1222.  
  1223. ;(defmacro extract-abs (start stop sound)
  1224.  
  1225. ;  `(let ($newsound $newstart)
  1226.  
  1227. ;     (progv '(*START* *STOP*)
  1228.  
  1229. ;            (list ,start ,stop)
  1230.  
  1231. ;        (setf $newstart *START*)
  1232.  
  1233. ;            (setf $newsound ,sound)            
  1234.  
  1235. ;            (setf $newsound (set-logical-stop-abs $newsound *STOP*)))
  1236.  
  1237. ;     (snd-xform $newsound (snd-srate $newsound) ,start ,stop 1.0)))
  1238.  
  1239.  
  1240.  
  1241.  
  1242.  
  1243. (defun local-to-global (local-time)
  1244.  
  1245.   (let ((d (warp-time *WARP*))
  1246.  
  1247.     (s (warp-stretch *WARP*))
  1248.  
  1249.     (w (warp-function *WARP*))
  1250.  
  1251.     global-time)
  1252.  
  1253.     (setf global-time (+ (* s local-time) d))
  1254.  
  1255.     (if w (snd-sref w global-time) global-time)))
  1256.  
  1257.  
  1258.  
  1259.  
  1260.  
  1261. (defmacro loud (x s)
  1262.  
  1263.  `(progv '(*LOUD*) (list (sum *LOUD* ,x))
  1264.  
  1265.      ,s))
  1266.  
  1267.  
  1268.  
  1269.  
  1270.  
  1271. (defmacro loud-abs (x s)
  1272.  
  1273.  `(progv '(*LOUD*) (list ,x)
  1274.  
  1275.      ,s))
  1276.  
  1277.  
  1278.  
  1279. (defun must-be-sound (x)
  1280.  
  1281.  (cond ((soundp x) x)
  1282.  
  1283.        (t
  1284.  
  1285.     (error "SOUND type expected" x))))
  1286.  
  1287.  
  1288.  
  1289. ;; SCALE-DB -- same as scale, but argument is in db
  1290.  
  1291. ;;
  1292.  
  1293. (defun scale-db (factor sound)
  1294.  
  1295.   (scale (db-to-linear factor) sound))
  1296.  
  1297.  
  1298.  
  1299. (defun set-control-srate (rate)
  1300.  
  1301.   (setf *default-control-srate* (float rate))
  1302.  
  1303.   (nyq:environment-init))
  1304.  
  1305.  
  1306.  
  1307. (defun set-sound-srate (rate) 
  1308.  
  1309.   (setf *default-sound-srate* (float rate))
  1310.  
  1311.   (nyq:environment-init))
  1312.  
  1313.  
  1314.  
  1315.  
  1316.  
  1317. ; s-plot -- compute and write n data points for plotting
  1318.  
  1319.  
  1320. (defun s-plot (snd &optional (n 1000))
  1321.  
  1322.   (prog ((points (snd-samples snd (1+ n)))
  1323.  
  1324.      (filename (soundfilename *default-plot-file*))
  1325.  
  1326.      outf
  1327.  
  1328.      (period (/ 1.0 (snd-srate snd)))
  1329.  
  1330.      len 
  1331.  
  1332.      (maximum 1.0))
  1333.  
  1334.     (setf outf (open filename :direction :output))
  1335.  
  1336.     (cond ((null outf)
  1337.  
  1338.        (format t "s-plot: could not open ~A!~%" filename)
  1339.  
  1340.        (return nil)))
  1341.  
  1342.     (format t "s-plot: writing ~A ... ~%" filename)
  1343.  
  1344.     (setf len (length points))
  1345.  
  1346.     (cond ((> len n)
  1347.  
  1348.        (setf len n)
  1349.  
  1350.        (format t "WARNING: SOUND TRUNCATED TO ~A POINTS~%" len)))
  1351.  
  1352.     (dotimes (i len)
  1353.  
  1354.       (cond ((< (abs maximum) (abs (aref points i)))
  1355.  
  1356.          (setf maximum (aref points i))))
  1357.  
  1358.       (format outf "~A ~A~%" (* i period) (aref points i)))
  1359.  
  1360.     (close outf)
  1361.  
  1362.     (cond ((> (abs maximum) 1.0)
  1363.  
  1364.        (format t "WARNING: MAXIMUM AMPLITUDE IS ~A~%" maximum)))
  1365.  
  1366.     (format t "~A points from ~A to ~A~%"
  1367.  
  1368.      len (snd-t0 snd) (+ (snd-t0 snd) (* len period)))))
  1369.  
  1370.  
  1371.  
  1372. ; run something like this to plot the points:
  1373.  
  1374. ; graph < points.dat | plot -Ttek
  1375.  
  1376.  
  1377.  
  1378.  
  1379.  
  1380. (defmacro sound-srate-abs (r s)
  1381.  
  1382.   `(progv '(*SOUND-SRATE*) (list ,r)
  1383.  
  1384.       ,s))
  1385.  
  1386.  
  1387.  
  1388.  
  1389.  
  1390. (defmacro stretch (x s)
  1391.  
  1392.  `(progv '(*WARP*) (list (list (warp-time *WARP*) 
  1393.  
  1394.                    (* (warp-stretch *WARP*) ,x)
  1395.  
  1396.                    (warp-function *WARP*)))
  1397.  
  1398.      (if (minusp (warp-stretch *WARP*))
  1399.  
  1400.          (break "Negative stretch factor is not allowed"))
  1401.  
  1402.              ,s))
  1403.  
  1404.  
  1405.  
  1406.          
  1407.  
  1408. (defmacro stretch-abs (x s)
  1409.  
  1410.  `(progv '(*WARP*) (list (list (local-to-global 0)
  1411.  
  1412.                    ,x
  1413.  
  1414.                    nil))
  1415.  
  1416.      (if (minusp (warp-stretch *WARP*))
  1417.  
  1418.          (break "Negative stretch factor is not allowed"))
  1419.  
  1420.              ,s))
  1421.  
  1422.  
  1423.  
  1424.  
  1425.  
  1426. (defmacro sustain (x s)
  1427.  
  1428.  `(progv '(*SUSTAIN*) (list (prod *SUSTAIN* ,x))
  1429.  
  1430.       ,s))
  1431.  
  1432.  
  1433.  
  1434.  
  1435.  
  1436. (defmacro sustain-abs (x s)
  1437.  
  1438.  `(progv '(*SUSTAIN*) (list ,x)
  1439.  
  1440.       ,s))
  1441.  
  1442.  
  1443.  
  1444.  
  1445.  
  1446. ;; (WARP-FUNCTION *WARP*) - extracts function field of warp triple
  1447.  
  1448. ;;
  1449.  
  1450. (setfn warp-function caddr)
  1451.  
  1452.  
  1453.  
  1454.  
  1455.  
  1456. ;; (WARP-STRETCH *WARP*) - extracts stretch field of warp triple
  1457.  
  1458. ;;
  1459.  
  1460. (setfn warp-stretch cadr)
  1461.  
  1462.  
  1463.  
  1464.  
  1465.  
  1466. ;; (WARP-TIME *WARP*) - extracts time field of warp triple
  1467.  
  1468. ;;
  1469.  
  1470. (setfn warp-time car)
  1471.  
  1472.  
  1473.  
  1474.  
  1475.  
  1476. (defmacro transpose (x s)
  1477.  
  1478.  `(progv '(*TRANSPOSE*) (list (sum *TRANSPOSE* ,x))
  1479.  
  1480.       ,s))
  1481.  
  1482.  
  1483.  
  1484.  
  1485.  
  1486. (defmacro transpose-abs (x s)
  1487.  
  1488.  `(progv '(*TRANSPOSE*) (list ,x)
  1489.  
  1490.       ,s))
  1491.  
  1492.  
  1493.  
  1494.  
  1495.  
  1496. ;; CONTROL-WARP -- apply a warp function to a control function
  1497.  
  1498. ;; 
  1499.  
  1500. (defun control-warp (warp-fn control &optional wrate)
  1501.  
  1502.   (cond (wrate
  1503.  
  1504.      (snd-resamplev control *control-srate*
  1505.  
  1506.             (snd-inverse warp-fn (local-to-global 0) wrate)))
  1507.  
  1508.     (t
  1509.  
  1510.      (snd-compose control
  1511.  
  1512.               (snd-inverse warp-fn (local-to-global 0) *control-srate*)))))
  1513.  
  1514.  
  1515.  
  1516.  
  1517.  
  1518. ;; (cue sound)
  1519.  
  1520. ;;    Cues the given sound; that is, it applies the current *WARP*, *LOUD*,
  1521.  
  1522. ;; *START*, and *STOP* values to the argument.  The logical start time is at
  1523.  
  1524. ;; local time 0.
  1525.  
  1526. (defun cue (sound)
  1527.  
  1528.   (cond ((arrayp sound)
  1529.  
  1530.      (let* ((len (length sound))
  1531.  
  1532.         (result (make-array len)))
  1533.  
  1534.         (dotimes (i len)
  1535.  
  1536.         (setf (aref result i)
  1537.  
  1538.               (cue-sound (aref sound i))))
  1539.  
  1540.         result))
  1541.  
  1542.     (t
  1543.  
  1544.      (cue-sound sound))))
  1545.  
  1546.  
  1547.  
  1548. (defun cue-sound (sound)
  1549.  
  1550.   (snd-xform sound
  1551.  
  1552.          (snd-srate sound)
  1553.  
  1554.          (local-to-global 0) *START* *STOP* (db-to-linear (get-loud))))
  1555.  
  1556.  
  1557.  
  1558. ;; (sound sound)
  1559.  
  1560. ;;    Same as (cue sound), except also warps the sound.
  1561.  
  1562. ;; Note that the *WARP* can change the pitch of the
  1563.  
  1564. ;; sound as a result of resampling.
  1565.  
  1566. ;; Here's the derivation for the warping code:
  1567.  
  1568. ;; *WARP* is a triple: (d s f) which denotes that the warp from local to
  1569.  
  1570. ;; global time is: f(st+d)
  1571.  
  1572. ;; We need to compose sound with the inverse of this to get a function
  1573.  
  1574. ;; of global time
  1575.  
  1576. ;; Let f-1 be the inverse of f.  Then the inverse of f(st+d) is 
  1577.  
  1578. ;; (f-1(t) - d)/s
  1579.  
  1580. ;; The composition gives us: (snd-compose sound (f-1(t) - d)/s)
  1581.  
  1582. ;; Eliminate the 1/s term by changing the sample rate of sound:
  1583.  
  1584. ;;  = (snd-compose (snd-scale-srate sound s) (f-1(t) - d))
  1585.  
  1586. ;; Eliminate the -d term by shifting f before taking the inverse:
  1587.  
  1588. ;;  = (snd-compose (scale-srate sound s) ((inverse f) - d))
  1589.  
  1590. ;;  = (snd-compose (scale-srate sound s) (inverse f(t + d)))
  1591.  
  1592. ;;  = (snd-compose (scale-srate sound s) (inverse (shift f -d)))
  1593.  
  1594. ;; snd-inverse takes a time and sample rate.  For time, use zero.
  1595.  
  1596. ;; The sample rate of inverse determines the final sample rate of
  1597.  
  1598. ;; this function, so use *SOUND-SRATE*:
  1599.  
  1600. ;;  = (snd-compose (scale-srate sound s) (snd-inverse (shift-time f (- d))
  1601.  
  1602. ;;                                              0 *SOUND-SRATE*))
  1603.  
  1604. ;;
  1605.  
  1606. (defun sound (sound)
  1607.  
  1608.    (cond ((null (warp-function *WARP*))
  1609.  
  1610.       (snd-xform sound (/ (snd-srate sound) (warp-stretch *WARP*))
  1611.  
  1612.              (local-to-global 0)
  1613.  
  1614.              *START* *STOP* (db-to-linear (get-loud))))
  1615.  
  1616.      (t
  1617.  
  1618.       (snd-compose (scale-srate sound (warp-stretch *WARP*))
  1619.  
  1620.                (snd-inverse (shift-time (warp-function *WARP*)
  1621.  
  1622.                         (- (warp-time *WARP*)))
  1623.  
  1624.                     0 *SOUND-SRATE*)))))
  1625.  
  1626.  
  1627.  
  1628.  
  1629.  
  1630. ;; (SCALE-SRATE SOUND SCALE)
  1631.  
  1632. ;; multiplies the sample rate by scale
  1633.  
  1634. (defun scale-srate (sound scale)
  1635.  
  1636.   (let ((new-srate (* scale (snd-srate sound))))
  1637.  
  1638.     (snd-xform sound new-srate (snd-time sound) 
  1639.  
  1640.            MIN-START-TIME MAX-STOP-TIME 1.0)))
  1641.  
  1642.  
  1643.  
  1644.  
  1645.  
  1646. ;; (SHIFT-TIME SOUND SHIFT)
  1647.  
  1648. ;; shift the time of a function by SHIFT, i.e. if SOUND is f(t),
  1649.  
  1650. ;; then (shift-time SOUND SHIFT) is f(t - SHIFT).  Note that if
  1651.  
  1652. ;; you look at plots, the shifted sound will move *right* when SHIFT
  1653.  
  1654. ;; is positive.  
  1655.  
  1656. (defun shift-time (sound shift)
  1657.  
  1658.   (snd-xform sound (snd-srate sound) (+ (snd-t0 sound) shift)
  1659.  
  1660.          MIN-START-TIME MAX-STOP-TIME 1.0))
  1661.  
  1662.  
  1663.  
  1664.  
  1665.  
  1666. ;; (NYQ:SOUND-TO-ARRAY SOUND N) - duplicate SOUND to N channels
  1667.  
  1668. ;;
  1669.  
  1670. (defun nyq:sound-to-array (sound n)
  1671.  
  1672.   (let ((result (make-array n)))
  1673.  
  1674.     (dotimes (i n)
  1675.  
  1676.       (setf (aref result i) sound))
  1677.  
  1678.     result))
  1679.  
  1680.  
  1681.  
  1682.  
  1683.  
  1684. ;; (control sound)
  1685.  
  1686. ;;    Same as (sound sound), except this is used for control signals.  
  1687.  
  1688. ;;    This code is identical to sound.
  1689.  
  1690. (setfn control sound)
  1691.  
  1692.  
  1693.  
  1694.  
  1695.  
  1696. ;; (env t1 t2 t4 l1 l2 l3 &optional duration)
  1697.  
  1698. ;; Creates a 4-phase envelope.
  1699.  
  1700. ;;    tN is the duration of phase N, and lN is the final level of
  1701.  
  1702. ;;    phase N.  t3 is implied by the duration, and l4 is 0.0.
  1703.  
  1704. ;;    If dur is not supplied, then 1.0 is assumed.  The envelope
  1705.  
  1706. ;;    duration is the product of dur, *STRETCH*, and *SUSTAIN*.  If 
  1707.  
  1708. ;;    t1 + t2 + 2ms + t4 > duration, then a two-phase envelope is
  1709.  
  1710. ;;    substituted that has an attack/release time ratio = t1/t4.
  1711.  
  1712. ;;    The sample rate of the returned sound is *CONTROL-SRATE*.
  1713.  
  1714. ;;
  1715.  
  1716. ;; Time transformation: the envelope is not warped; the start time and
  1717.  
  1718. ;; stop times are warped to global time.  Then the value of *SUSTAIN* at
  1719.  
  1720. ;; the begining of the envelope is used to determing absolute duration.
  1721.  
  1722. ;; Since PWL is ultimately called to create the envelope, we must use
  1723.  
  1724. ;; ABS-ENV to prevent any further transforms inside PWL.  We use
  1725.  
  1726. ;; (AT global-start ...) inside ABS-ENV so that the final result has 
  1727.  
  1728. ;; the proper starting time.
  1729.  
  1730. ;;
  1731.  
  1732. (defun env (t1 t2 t4 l1 l2 l3 &optional (duration 1.0))
  1733.  
  1734.   (let (actual-dur min-dur ratio t3
  1735.  
  1736.     (actual-dur (get-duration duration)))
  1737.  
  1738.     (setf min-dur (+ t1 t2 t4 0.002))
  1739.  
  1740.     (cond ((< actual-dur min-dur)
  1741.  
  1742.        (setf ratio (/ t1 (+ t1 t4)))
  1743.  
  1744.        (setf t1 (* ratio actual-dur))
  1745.  
  1746.        (setf t2 (- actual-dur t1))
  1747.  
  1748.        (setf t3 0.0)
  1749.  
  1750.        (setf t4 0.0)
  1751.  
  1752.        (setf l2 0.0)
  1753.  
  1754.        (setf l3 0.0))
  1755.  
  1756.       (t
  1757.  
  1758.        (setf t3 (- actual-dur t1 t2 t4))))
  1759.  
  1760.     (set-logical-stop
  1761.  
  1762.      (abs-env (at *rslt*
  1763.  
  1764.           (pwl t1 l1 (+ t1 t2) l2 (- actual-dur t4) l3 actual-dur)))
  1765.  
  1766.      duration)))
  1767.  
  1768.  
  1769.  
  1770.  
  1771.  
  1772. (defun gate (sound lookahead risetime falltime floor threshold)
  1773.  
  1774.     (cond ((< lookahead risetime)
  1775.  
  1776.            (break "lookahead must be greater than risetime in GATE function"))
  1777.  
  1778.           ((or (< risetime 0) (< falltime 0) (< floor 0))
  1779.  
  1780.            (break "risetime, falltime, and floor must all be positive in GATE function"))
  1781.  
  1782.           (t
  1783.  
  1784.            (let ((s
  1785.  
  1786.               (snd-gate (seq (cue sound) (abs-env (s-rest lookahead)))
  1787.  
  1788.                     lookahead risetime falltime floor threshold)))
  1789.  
  1790.              (snd-xform s (snd-srate s) (snd-t0 sound) 
  1791.  
  1792.             (+ (snd-t0 sound) lookahead) MAX-STOP-TIME 1.0)))))
  1793.  
  1794.  
  1795.  
  1796.  
  1797.  
  1798. ;; (osc-note step &optional duration env sust volume sound)
  1799.  
  1800. ;;   Creates a note using table-lookup osc, but with an envelope.
  1801.  
  1802. ;; The ENV parameter may be a parameter list for the env function,
  1803.  
  1804. ;; or it may be a sound.
  1805.  
  1806. ;;
  1807.  
  1808. (defun osc-note (pitch &optional (duration 1.0) 
  1809.  
  1810.                (env-spec '(0.02 0.1 0.3 1.0 .8 .7))
  1811.  
  1812.                (volume 0.0)
  1813.  
  1814.                (table *TABLE*))
  1815.  
  1816.   (set-logical-stop
  1817.  
  1818.    (mult (loud volume (osc pitch duration table))
  1819.  
  1820.      (if (listp env-spec)
  1821.  
  1822.        (apply 'env env-spec)
  1823.  
  1824.        env-spec))
  1825.  
  1826.    duration))
  1827.  
  1828.  
  1829.  
  1830.  
  1831.  
  1832. ;; force-srate -- resample snd if necessary to get sample rate
  1833.  
  1834. ;
  1835.  
  1836. (defun force-srate (sr snd)
  1837.  
  1838.   (cond ((not (numberp sr))
  1839.  
  1840.      (error "force-srate: SR should be a number")))
  1841.  
  1842.   (cond ((arrayp snd)
  1843.  
  1844.      (let* ((len (length snd))
  1845.  
  1846.         (result (make-array len)))
  1847.  
  1848.        (dotimes (i len)
  1849.  
  1850.             (setf (aref result i) 
  1851.  
  1852.               (force-srate sr (aref snd i))))
  1853.  
  1854.        result))
  1855.  
  1856.     (t
  1857.  
  1858.      (let ((snd-sr (snd-srate snd)))
  1859.  
  1860.        (cond ((> sr snd-sr) (snd-up sr snd))
  1861.  
  1862.          ((< sr snd-sr) (snd-down sr snd))
  1863.  
  1864.          (t snd))))))
  1865.  
  1866.  
  1867.  
  1868.  
  1869.  
  1870. (defun force-srates (srs snd)
  1871.  
  1872.   (cond ((and (numberp srs) (soundp snd))
  1873.  
  1874.      (force-srate srs snd))
  1875.  
  1876.     ((and (arrayp srs) (arrayp snd))
  1877.  
  1878.      (let* ((len (length snd))
  1879.  
  1880.         (result (make-array len)))
  1881.  
  1882.        (dotimes (i len)
  1883.  
  1884.             (setf (aref result i) 
  1885.  
  1886.               (force-srate (aref srs i) (aref snd i))))
  1887.  
  1888.        result))
  1889.  
  1890.     (t (error "arguments not compatible"))))
  1891.  
  1892.  
  1893.  
  1894.  
  1895.  
  1896. ;; (breakpoints-convert (t1 x1 t2 x2 ... tn) t0)
  1897.  
  1898. ;;   converts times to sample numbers and scales amplitudes
  1899.  
  1900. ;;   t0 is the global (after warping) start time
  1901.  
  1902. ;;
  1903.  
  1904. ;; NOTE: there were some stack overflow problems with the original
  1905.  
  1906. ;; recursive version (in comments now), so it was rewritten as an
  1907.  
  1908. ;; iteration.
  1909.  
  1910. ;;
  1911.  
  1912. (defun breakpoints-convert (list t0)
  1913.  
  1914.   (prog (sample-count result sust (last-count 0))
  1915.  
  1916.     (setf sust (get-sustain))
  1917.  
  1918.  loop
  1919.  
  1920.     (setf sample-count 
  1921.  
  1922.       (truncate (+ 0.5 (* (- (local-to-global (* (car list) sust)) t0)
  1923.  
  1924.                  *control-srate*))))
  1925.  
  1926.     ; now we have a new sample count to put into result list
  1927.  
  1928.     ; make sure result is non-decreasing
  1929.  
  1930.     (cond ((< sample-count last-count)
  1931.  
  1932.        (setf sample-count last-count)))
  1933.  
  1934.     (setf last-count sample-count)
  1935.  
  1936.     (push sample-count result)
  1937.  
  1938.     (cond ((cdr list)
  1939.  
  1940.        (setf list (cdr list))
  1941.  
  1942.        (push (float (car list)) result)))
  1943.  
  1944.     (setf list (cdr list))
  1945.  
  1946.     (cond (list
  1947.  
  1948.        (go loop)))
  1949.  
  1950.     (return (reverse result))))
  1951.  
  1952.  
  1953.  
  1954.       
  1955.  
  1956.  
  1957.  
  1958. ;; (pwl t1 l1 t2 l2 ... tn)
  1959.  
  1960. ;;   Creates a piece-wise linear envelope from breakpoint data.
  1961.  
  1962. ;;
  1963.  
  1964. (defun pwl (&rest breakpoints) (pwl-list breakpoints))
  1965.  
  1966.  
  1967.  
  1968. (defun pwlr (&rest breakpoints) (pwlr-list breakpoints))
  1969.  
  1970.  
  1971.  
  1972. ;; (breakpoints-relative list)
  1973.  
  1974. ;;  converts list, which has the form (value dur value dur value ...)
  1975.  
  1976. ;;  into the form (value time value time value ...)
  1977.  
  1978. ;;  the list may have an even or odd length
  1979.  
  1980. ;;
  1981.  
  1982. (defun breakpoints-relative (breakpoints)
  1983.  
  1984.   (prog (result (sum 0.0))
  1985.  
  1986.  loop
  1987.  
  1988.      (cond (breakpoints
  1989.  
  1990.         (push (car breakpoints) result)
  1991.  
  1992.         (setf breakpoints (cdr breakpoints))
  1993.  
  1994.         (cond (breakpoints
  1995.  
  1996.            (setf sum (+ sum (car breakpoints)))
  1997.  
  1998.            (push sum result)
  1999.  
  2000.            (setf breakpoints (cdr breakpoints))
  2001.  
  2002.            (go loop)))))
  2003.  
  2004.      (return (reverse result))))
  2005.  
  2006.  
  2007.  
  2008.  
  2009.  
  2010. (defun breakpoints-relative (breakpoints)
  2011.  
  2012.   (prog (result (sum 0.0))
  2013.  
  2014.  loop
  2015.  
  2016.     (setf sum (+ sum (car breakpoints)))
  2017.  
  2018.     (push sum result)
  2019.  
  2020.     (cond ((cdr breakpoints)
  2021.  
  2022.        (setf breakpoints (cdr breakpoints))
  2023.  
  2024.        (push (car breakpoints) result)))
  2025.  
  2026.     (setf breakpoints (cdr breakpoints))
  2027.  
  2028.     (cond (breakpoints
  2029.  
  2030.        (go loop)))
  2031.  
  2032.     (return (reverse result))))
  2033.  
  2034.  
  2035.  
  2036.  
  2037.  
  2038. (defun pwlr-list (breakpoints)
  2039.  
  2040.   (pwl-list (breakpoints-relative breakpoints)))
  2041.  
  2042.  
  2043.  
  2044. (defun pwl-list (breakpoints)
  2045.  
  2046.   (let ((t0 (local-to-global 0)))
  2047.  
  2048.     (snd-pwl t0 *control-srate* (breakpoints-convert breakpoints t0))))
  2049.  
  2050.  
  2051.  
  2052. ;; (pwlv l1 t1 l2 t2 ... ln)
  2053.  
  2054. ;; Creates a piece-wise linear envelope from breakpoint data;
  2055.  
  2056. ;; the function initial and final values are explicit
  2057.  
  2058. ;;
  2059.  
  2060. (defun pwlv (&rest breakpoints)
  2061.  
  2062.   ;use pwl, modify breakpoints with initial and final changes
  2063.  
  2064.   ;need to put initial time of 0, and final time of 0
  2065.  
  2066.   (pwlv-list breakpoints))
  2067.  
  2068.  
  2069.  
  2070. (defun pwlv-list (breakpoints)
  2071.  
  2072.     (pwl-list (cons 0.0 (append breakpoints '(0.0)))))
  2073.  
  2074.  
  2075.  
  2076. (defun pwlvr (&rest breakpoints) (pwlvr-list breakpoints))
  2077.  
  2078.  
  2079.  
  2080. (defun pwlvr-list (breakpoints)
  2081.  
  2082.   (pwlr-list (cons 0.0 (append breakpoints '(0.0)))))
  2083.  
  2084.  
  2085.  
  2086. (defun pwe (&rest breakpoints)
  2087.  
  2088.   (pwe-list breakpoints))
  2089.  
  2090.  
  2091.  
  2092. (defun pwe-list (breakpoints)
  2093.  
  2094.   (pwev-list (cons 1.0 (append breakpoints '(1.0)))))
  2095.  
  2096.  
  2097.  
  2098. (defun pwer (&rest breakpoints) (pwer-list breakpoints))
  2099.  
  2100.  
  2101.  
  2102. (defun pwer-list (breakpoints)
  2103.  
  2104.   (pwe-list (breakpoints-relative breakpoints)))
  2105.  
  2106.  
  2107.  
  2108. (defun pwev (&rest breakpoints)
  2109.  
  2110.   (pwev-list breakpoints))
  2111.  
  2112.  
  2113.  
  2114. (defun pwev-list (breakpoints)
  2115.  
  2116.   (let ((lis (breakpoints-log breakpoints)))
  2117.  
  2118.     (s-exp (pwl-list lis))))
  2119.  
  2120.  
  2121.  
  2122. (defun pwevr (&rest breakpoints) (pwevr-list breakpoints))
  2123.  
  2124.  
  2125.  
  2126. (defun pwevr-list (breakpoints)
  2127.  
  2128.   (pwev-list (cdr (breakpoints-relative (cons 0.0 breakpoints)))))
  2129.  
  2130.  
  2131.  
  2132.  
  2133.  
  2134. (defun breakpoints-log (breakpoints)
  2135.  
  2136.   (prog ((result '(0.0)) val tim)
  2137.  
  2138. loop
  2139.  
  2140.     (cond (breakpoints
  2141.  
  2142.        (setf val (float (car breakpoints)))
  2143.  
  2144.        (setf breakpoints (cdr breakpoints))
  2145.  
  2146.        (cond (breakpoints
  2147.  
  2148.           (setf tim (car breakpoints))
  2149.  
  2150.           (setf breakpoints (cdr breakpoints))))
  2151.  
  2152.        (setf result (cons tim (cons (log val) result)))
  2153.  
  2154.        (cond ((null breakpoints)
  2155.  
  2156.           (return (reverse result))))
  2157.  
  2158.        (go loop))
  2159.  
  2160.       (t
  2161.  
  2162.        (error "Expected odd number of elements in breakpoint list")))))
  2163.  
  2164.  
  2165.  
  2166.  
  2167.  
  2168. ;; SOUND-WARP -- apply warp function to a sound
  2169.  
  2170. ;; 
  2171.  
  2172. (defun sound-warp (warp-fn signal &optional wrate)
  2173.  
  2174.   (cond (wrate
  2175.  
  2176.      (snd-resamplev signal *sound-srate*
  2177.  
  2178.             (snd-inverse warp-fn (local-to-global 0) wrate)))
  2179.  
  2180.     (t
  2181.  
  2182.      (snd-compose signal 
  2183.  
  2184.               (snd-inverse warp-fn (local-to-global 0) *sound-srate*)))))
  2185.  
  2186.  
  2187.  
  2188. (defun snd-extent (sound maxsamples) 
  2189.  
  2190.     (list (snd-t0 sound)
  2191.  
  2192.       (+ (snd-t0 sound) (/ (snd-length sound maxsamples)
  2193.  
  2194.                    (snd-srate sound)))))
  2195.  
  2196.  
  2197.  
  2198. (setfn snd-flatten snd-length)
  2199.  
  2200.  
  2201.  
  2202. ;; (maketable sound)
  2203.  
  2204. ;;   Creates a table for osc, lfo, etc. by assuming that the samples
  2205.  
  2206. ;;   in sound represent one period.  The sound must start at time 0.
  2207.  
  2208.  
  2209.  
  2210. (defun maketable (sound)
  2211.  
  2212.   (list sound
  2213.  
  2214.     (hz-to-step 
  2215.  
  2216.      (/ 1.0
  2217.  
  2218.         (cadr (snd-extent sound 1000000))))
  2219.  
  2220.     T))
  2221.  
  2222.  
  2223.  
  2224.  
  2225.  
  2226. ;(defmacro endTime (sound)
  2227.  
  2228. ;   `(get-logical-stop ,sound))
  2229.  
  2230.  
  2231.  
  2232.  
  2233.  
  2234. ;(defmacro beginTime (sound)
  2235.  
  2236. ;   `(car (snd-extent ,sound)))
  2237.  
  2238.  
  2239.  
  2240.  
  2241.  
  2242. ; simple stereo pan: as where goes from 0 to 1, sound
  2243.  
  2244. ; is linearly panned from left to right
  2245.  
  2246. ;
  2247.  
  2248. (defun pan (sound where)
  2249.  
  2250.   (vector (mult sound (sum 1 (mult -1 where)))
  2251.  
  2252.       (mult sound where)))
  2253.  
  2254.  
  2255.  
  2256.  
  2257.  
  2258. (defun prod (&rest snds)
  2259.  
  2260.   (cond ((null snds)
  2261.  
  2262.      (snd-zero (local-to-global 0) *sound-srate*))
  2263.  
  2264.     ((null (cdr snds))
  2265.  
  2266.      (car snds))
  2267.  
  2268.     ((null (cddr snds))
  2269.  
  2270.      (nyq:prod2 (car snds) (cadr snds)))
  2271.  
  2272.     (t
  2273.  
  2274.      (nyq:prod2 (car snds) (apply #'prod (cdr snds))))))
  2275.  
  2276.  
  2277.  
  2278. (setfn mult prod)
  2279.  
  2280.  
  2281.  
  2282.  
  2283.  
  2284. ;; (NYQ:PROD-OF-ARRAYS S1 S2) - form pairwise products
  2285.  
  2286. ;
  2287.  
  2288. (defun nyq:prod-of-arrays (s1 s2)
  2289.  
  2290.   (let* ((n (length s1))
  2291.  
  2292.      (p (make-array n)))
  2293.  
  2294.     (cond ((/= n (length s2))
  2295.  
  2296.        (error "unequal number of channels in prod")))
  2297.  
  2298.     (dotimes (i n)
  2299.  
  2300.       (setf (aref p i) (nyq:prod2 (aref s1 i) (aref s2 i))))
  2301.  
  2302.     p))
  2303.  
  2304.  
  2305.  
  2306.  
  2307.  
  2308. ; nyq:prod2 - multiply two arguments
  2309.  
  2310.  
  2311. (defun nyq:prod2 (s1 s2)
  2312.  
  2313.   (setf s1 (nyq:coerce-to s1 s2))
  2314.  
  2315.   (setf s2 (nyq:coerce-to s2 s1))
  2316.  
  2317.   (cond ((arrayp s1)
  2318.  
  2319.      (nyq:prod-of-arrays s1 s2))
  2320.  
  2321.     (t
  2322.  
  2323.      (nyq:prod-2-sounds s1 s2))))
  2324.  
  2325.  
  2326.  
  2327.  
  2328.  
  2329. ; (PROD-2-SOUNDS S1 S2) - multiply two sound arguments
  2330.  
  2331.  
  2332. (defun nyq:prod-2-sounds (s1 s2)
  2333.  
  2334.   (cond ((numberp s1)
  2335.  
  2336.      (cond ((numberp s2)
  2337.  
  2338.         (* s1 s2))
  2339.  
  2340.            (t
  2341.  
  2342.         (scale s1 s2))))
  2343.  
  2344.     ((numberp s2)
  2345.  
  2346.      (scale s2 s1))
  2347.  
  2348.     (t
  2349.  
  2350.      (let ((s1sr (snd-srate s1))
  2351.  
  2352.            (s2sr (snd-srate s2)))
  2353.  
  2354. ;    (display "nyq:prod-2-sounds" s1sr s2sr)
  2355.  
  2356.         (cond ((> s1sr s2sr)
  2357.  
  2358.            (snd-prod s1 (snd-up s1sr s2)))
  2359.  
  2360.           ((< s1sr s2sr)
  2361.  
  2362.            (snd-prod (snd-up s2sr s1) s2))
  2363.  
  2364.           (t
  2365.  
  2366.            (snd-prod s1 s2)))))))
  2367.  
  2368.  
  2369.  
  2370.  
  2371.  
  2372. ;; RAMP -- linear ramp from 0 to x
  2373.  
  2374. ;;
  2375.  
  2376. (defun ramp (&optional (x 1))
  2377.  
  2378.   (let* ((duration (get-duration x)))
  2379.  
  2380.     (warp-abs nil 
  2381.  
  2382.     (at *rslt*
  2383.  
  2384.         (sustain-abs 1
  2385.  
  2386.         (pwl duration 1 (+ duration (/ *control-srate*))))))))
  2387.  
  2388.  
  2389.  
  2390.  
  2391.  
  2392. (defun resample (snd rate)
  2393.  
  2394.   (cond ((arrayp snd)
  2395.  
  2396.      (let* ((len (length snd))
  2397.  
  2398.         (result (make-array len)))
  2399.  
  2400.         (dotimes (i len)
  2401.  
  2402.         (setf (aref result i)
  2403.  
  2404.               (snd-resample (aref snd i) rate)))
  2405.  
  2406.         result))
  2407.  
  2408.     (t
  2409.  
  2410.      (snd-resample snd rate))))
  2411.  
  2412.  
  2413.  
  2414.  
  2415.  
  2416. (defun scale (amt snd)
  2417.  
  2418.   (cond ((arrayp snd)
  2419.  
  2420.      (let* ((len (length snd))
  2421.  
  2422.         (result (make-array len)))
  2423.  
  2424.         (dotimes (i len)
  2425.  
  2426.         (setf (aref result i) (snd-scale amt (aref snd i))))
  2427.  
  2428.         result))
  2429.  
  2430.     (t
  2431.  
  2432.      (snd-scale amt snd))))
  2433.  
  2434.  
  2435.  
  2436.  
  2437.  
  2438. (setfn s-print-tree snd-print-tree)
  2439.  
  2440.  
  2441.  
  2442. ;; (PEAK sound-expression number-of-samples) - find peak amplitude
  2443.  
  2444. ;
  2445.  
  2446. ; NOTE: this used to be called s-max
  2447.  
  2448. ;
  2449.  
  2450. (defmacro peak (expression maxlen)
  2451.  
  2452.    `(snd-max ',expression ,maxlen))
  2453.  
  2454.  
  2455.  
  2456. ;; (S-MAX S1 S2) - return maximum of S1, S2
  2457.  
  2458. ;
  2459.  
  2460. (defun s-max (s1 s2)
  2461.  
  2462.   (setf s1 (nyq:coerce-to s1 s2))
  2463.  
  2464.   (setf s2 (nyq:coerce-to s2 s1))
  2465.  
  2466.   (cond ((arrayp s1)
  2467.  
  2468.      (nyq:max-of-arrays s1 s2))
  2469.  
  2470.     (t
  2471.  
  2472.      (nyq:max-2-sounds s1 s2))))
  2473.  
  2474.  
  2475.  
  2476. (defun nyq:max-of-arrays (s1 s2)
  2477.  
  2478.   (let* ((n (length s1))
  2479.  
  2480.      (p (make-array n)))
  2481.  
  2482.     (cond ((/= n (length s2))
  2483.  
  2484.        (error "unequal number of channels in max")))
  2485.  
  2486.     (dotimes (i n)
  2487.  
  2488.       (setf (aref p i) (s-max (aref s1 i) (aref s2 i))))
  2489.  
  2490.     p))
  2491.  
  2492.  
  2493.  
  2494. (defun nyq:max-2-sounds (s1 s2)
  2495.  
  2496.   (cond ((numberp s1)
  2497.  
  2498.          (cond ((numberp s2)
  2499.  
  2500.                 (max s1 s2))
  2501.  
  2502.                (t
  2503.  
  2504.                 (snd-maxv s2
  2505.  
  2506.                           (snd-const s1 (local-to-global 0.0)
  2507.  
  2508.                                      (snd-srate s2) (get-duration 1.0))))))
  2509.  
  2510.         ((numberp s2)
  2511.  
  2512.          (snd-maxv s1 (snd-const s2 (local-to-global 0.0)
  2513.  
  2514.                    (snd-srate s1) (get-duration 1.0))))
  2515.  
  2516.         (t
  2517.  
  2518.          (let ((s1sr (snd-srate s1))
  2519.  
  2520.                (s2sr (snd-srate s2)))
  2521.  
  2522.             (cond ((> s1sr s2sr)
  2523.  
  2524.                    (snd-maxv s1 (snd-up s1sr s2)))
  2525.  
  2526.                   ((< s1sr s2sr)
  2527.  
  2528.                    (snd-maxv (snd-up s2sr s1) s2))
  2529.  
  2530.                   (t
  2531.  
  2532.                    (snd-maxv s1 s2)))))))
  2533.  
  2534.  
  2535.  
  2536. (defun s-min (s1 s2)
  2537.  
  2538.   (setf s1 (nyq:coerce-to s1 s2))
  2539.  
  2540.   (setf s2 (nyq:coerce-to s2 s1))
  2541.  
  2542.   (cond ((arrayp s1)
  2543.  
  2544.          (nyq:min-of-arrays s1 s2))
  2545.  
  2546.         (t
  2547.  
  2548.          (nyq:min-2-sounds s1 s2))))
  2549.  
  2550.  
  2551.  
  2552. (defun nyq:min-of-arrays (s1 s2)
  2553.  
  2554.   (let* ((n (length s1))
  2555.  
  2556.      (p (make-array n)))
  2557.  
  2558.     (cond ((/= n (length s2))
  2559.  
  2560.        (error "unequal number of channels in max")))
  2561.  
  2562.     (dotimes (i n)
  2563.  
  2564.       (setf (aref p i) (s-min (aref s1 i) (aref s2 i))))
  2565.  
  2566.     p))
  2567.  
  2568.  
  2569.  
  2570. (defun nyq:min-2-sounds (s1 s2)
  2571.  
  2572.   (cond ((numberp s1)
  2573.  
  2574.      (cond ((numberp s2)
  2575.  
  2576.         (min s1 s2))
  2577.  
  2578.            (t
  2579.  
  2580.         (snd-minv
  2581.  
  2582.          (snd-const s1 (local-to-global 0.0)
  2583.  
  2584.                 (snd-srate s2) (get-duration 1.0))))))
  2585.  
  2586.     ((numberp s2)
  2587.  
  2588.      (snd-minv (snd-const s2 (local-to-global 0.0)
  2589.  
  2590.                 (snd-srate s1) (get-duration 1.0))))
  2591.  
  2592.     (t
  2593.  
  2594.      (let ((s1sr (snd-srate s1))
  2595.  
  2596.            (s2sr (snd-srate s2)))
  2597.  
  2598.         (cond ((> s1sr s2sr)
  2599.  
  2600.            (snd-minv s1 (snd-up s1sr s2)))
  2601.  
  2602.           ((< s1sr s2sr)
  2603.  
  2604.            (snd-minv (snd-up s2sr s1) s2))
  2605.  
  2606.           (t
  2607.  
  2608.            (snd-minv s1 s2)))))))
  2609.  
  2610.  
  2611.  
  2612. (defun snd-minv (s1 s2)
  2613.  
  2614.   (scale -1.0 (snd-maxv (scale -1.0 s1) (scale -1.0 s2))))
  2615.  
  2616.  
  2617.  
  2618. ; sequence macros SEQ and SEQREP are now in seq.lsp:
  2619.  
  2620.  
  2621. (load "seq" :verbose NIL)
  2622.  
  2623.  
  2624.  
  2625. ; set-logical-stop - modify the sound and return it, time is shifted and
  2626.  
  2627. ;             stretched
  2628.  
  2629. (defun set-logical-stop (snd tim)
  2630.  
  2631.   (let ((d (local-to-global tim)))
  2632.  
  2633.     (multichan-expand #'set-logical-stop-abs snd d)))
  2634.  
  2635.  
  2636.  
  2637.  
  2638.  
  2639. ; set-logical-stop-abs - modify the sound and return it
  2640.  
  2641.  
  2642. (defun set-logical-stop-abs (snd tim) (snd-set-logical-stop snd tim) snd)
  2643.  
  2644.  
  2645.  
  2646.  
  2647.  
  2648. (defmacro simrep (pair sound)
  2649.  
  2650.   `(let (_snds)
  2651.  
  2652.      (dotimes ,pair (push ,sound _snds))
  2653.  
  2654.      (sim-list _snds)))
  2655.  
  2656.  
  2657.  
  2658. (defun sim (&rest snds)
  2659.  
  2660.   (sim-list snds))
  2661.  
  2662.  
  2663.  
  2664. (setfn sum sim)
  2665.  
  2666.  
  2667.  
  2668. (defun sim-list (snds)
  2669.  
  2670.   (cond ((null snds)
  2671.  
  2672.      (snd-zero (local-to-global 0) *sound-srate*))
  2673.  
  2674.     ((null (cdr snds))
  2675.  
  2676.      (car snds))
  2677.  
  2678.     ((null (cddr snds))
  2679.  
  2680.      (nyq:add2 (car snds) (cadr snds)))
  2681.  
  2682.     (t
  2683.  
  2684.      (nyq:add2 (car snds) (sim-list (cdr snds))))))
  2685.  
  2686.  
  2687.  
  2688.  
  2689.  
  2690. ;(defun rest (&optional (dur 1.0))
  2691.  
  2692. ;  (cue (set-Logical-stop (* dur *stretch*) (s-create))))
  2693.  
  2694. (defun s-rest (&optional (dur 1.0))
  2695.  
  2696.   (let ((d (get-duration dur)))
  2697.  
  2698.     (snd-const 0.0 *rslt* *SOUND-SRATE* d)))
  2699.  
  2700.  
  2701.  
  2702.  
  2703.  
  2704. (defun tempo (warpfn)
  2705.  
  2706.   (slope (snd-inverse warpfn (local-to-global 0) *control-srate*)))
  2707.  
  2708.  
  2709.  
  2710.  
  2711.  
  2712.  
  2713.  
  2714. ;; (SUM-OF-ARRAYS S1 S2) - add multichannel sounds
  2715.  
  2716.  
  2717. ; result has as many channels the largest of s1, s2
  2718.  
  2719. ; corresponding channels are added, extras are copied
  2720.  
  2721.  
  2722. (defun sum-of-arrays (s1 s2)
  2723.  
  2724.   (let* ((n1 (length s1))
  2725.  
  2726.      (n2 (length s2))
  2727.  
  2728.      (n (min n1 n2))
  2729.  
  2730.      (m (max n1 n2))
  2731.  
  2732.      (result (make-array m))
  2733.  
  2734.      (big-s (if (> n1 n2) s1 s2)))
  2735.  
  2736.     
  2737.  
  2738.     (dotimes (i n)
  2739.  
  2740.       (setf (aref result i) (nyq:add-2-sounds (aref s1 i) (aref s2 i))))
  2741.  
  2742.     (dotimes (i (- m n))
  2743.  
  2744.       (setf (aref result (+ n i)) (aref big-s (+ n i))))
  2745.  
  2746.     result))
  2747.  
  2748.  
  2749.  
  2750.  
  2751.  
  2752. ;; (WARP fn behavior) - warp behavior according to fn
  2753.  
  2754. ;;
  2755.  
  2756. ;; fn is a map from behavior time to local time, and *WARP* expresses
  2757.  
  2758. ;; a map from local to global time.
  2759.  
  2760. ;; To produce a new *WARP* for the environment, we want to compose the
  2761.  
  2762. ;; effect of the current *WARP* with fn.  Note that fn is also a behavior.
  2763.  
  2764. ;; It is evaluated in the current environment first, then it is used to
  2765.  
  2766. ;; modify the environment seen by behavior.
  2767.  
  2768. ;; *WARP* is a triple: (d s f) denoting the function f(st+d).
  2769.  
  2770. ;; Letting g represent the new warp function fn, we want f(st+d) o g, or
  2771.  
  2772. ;; f(s*g(t) + d) in the form (d' s' f').
  2773.  
  2774. ;; Let's do this one step at a time:
  2775.  
  2776. ;; f(s*g(t) + d) = f(scale(s, g) + d)
  2777.  
  2778. ;;               = (shift f -d)(scale(s, g))
  2779.  
  2780. ;;               = (snd-compose (shift-time f (- d)) (scale s g))
  2781.  
  2782. ;;
  2783.  
  2784. ;; If f in NIL, it denotes the identity mapping f(t)=t, so we can
  2785.  
  2786. ;; simplify:
  2787.  
  2788. ;; f(scale(s, g) + d) = scale(s, g) + d
  2789.  
  2790. ;;                    = (snd-offset (scale s g) d)
  2791.  
  2792.  
  2793.  
  2794. (defmacro warp (x s)
  2795.  
  2796.  `(progv '(*WARP*) (list 
  2797.  
  2798.             (list 0.0 1.0
  2799.  
  2800.               (if (warp-function *WARP*)
  2801.  
  2802.                   (snd-compose (shift-time (warp-function *WARP*) 
  2803.  
  2804.                                (- (warp-time *WARP*)))
  2805.  
  2806.                        (scale (warp-stretch *WARP*) 
  2807.  
  2808.                           (must-be-sound ,x)))
  2809.  
  2810.                   (snd-offset (scale (warp-stretch *WARP*) 
  2811.  
  2812.                          (must-be-sound ,x))
  2813.  
  2814.                       (warp-time *WARP*)))))
  2815.  
  2816.      ,s))
  2817.  
  2818.  
  2819.  
  2820.  
  2821.  
  2822. (defmacro warp-abs (x s)
  2823.  
  2824.  `(progv '(*WARP*) (list (list 0.0 1.0 ,x))
  2825.  
  2826.      ,s))
  2827.  
  2828.  
  2829.  
  2830.  
  2831.  
  2832. ;; MULTICHAN-EXPAND -- construct and return array according to args
  2833.  
  2834. ;;
  2835.  
  2836. ;; arrays are used in Nyquist to represent multiple channels
  2837.  
  2838. ;; if any argument is an array, make sure all array arguments
  2839.  
  2840. ;; have the same length.  Then, construct a multichannel result
  2841.  
  2842. ;; by calling fn once for each channel.  The arguments passed to
  2843.  
  2844. ;; fn for the i'th channel are either the i'th element of an array
  2845.  
  2846. ;; argument, or just a copy of a non-array argument.
  2847.  
  2848. ;;
  2849.  
  2850. (defun multichan-expand (fn &rest args)
  2851.  
  2852.   (let (len newlen result) ; len is a flag as well as a count
  2853.  
  2854.     (dolist (a args)
  2855.  
  2856.         (cond ((arrayp a)
  2857.  
  2858.            (setf newlen (length a))
  2859.  
  2860.            (cond ((and len (/= len newlen))
  2861.  
  2862.               (error (format nil "In ~A, two arguments are vectors of differing length." fn))))
  2863.  
  2864.            (setf len newlen))))
  2865.  
  2866.     (cond (len
  2867.  
  2868.        (setf result (make-array len))
  2869.  
  2870.        ; for each channel, call fn with args
  2871.  
  2872.        (dotimes (i len)
  2873.  
  2874.            (setf (aref result i)
  2875.  
  2876.              (apply fn
  2877.  
  2878.             (mapcar
  2879.  
  2880.                 #'(lambda (a)
  2881.  
  2882.                 ; take i'th entry or replicate:
  2883.  
  2884.                 (cond ((arrayp a) (aref a i))
  2885.  
  2886.                       (t a)))
  2887.  
  2888.                 args))))
  2889.  
  2890.        result)
  2891.  
  2892.       (t
  2893.  
  2894.        (apply fn args)))))
  2895.  
  2896.  
  2897.  
  2898.  
  2899.  
  2900. ;; SELECT-IMPLEMENTATION-? -- apply an implementation according to args
  2901.  
  2902. ;;
  2903.  
  2904. ;; There is a different Nyquist primitive for each combination of 
  2905.  
  2906. ;; constant (NUMBERP) and time-variable (SOUNDP) arguments.  E.g.
  2907.  
  2908. ;; a filter with fixed parameters differs from one with varying
  2909.  
  2910. ;; parameters.  In most cases, the user just calls one function,
  2911.  
  2912. ;; and the arguments are decoded here:
  2913.  
  2914.  
  2915.  
  2916.  
  2917.  
  2918. ;; SELECT-IMPLEMENTATION-1-1 -- 1 sound arg, 1 selector
  2919.  
  2920. ;;
  2921.  
  2922. (defun select-implementation-1-1 (fns snd sel1 &rest others)
  2923.  
  2924.   (if (numberp sel1)
  2925.  
  2926.     (apply (aref fns 0) (cons snd (cons sel1 others)))
  2927.  
  2928.     (apply (aref fns 1) (cons snd (cons sel1 others)))))
  2929.  
  2930.  
  2931.  
  2932.  
  2933.  
  2934. ;; SELECT-IMPLEMENTATION-1-2 -- 1 sound arg, 2 selectors
  2935.  
  2936. ;;
  2937.  
  2938. ;; choose implemenation according to args 2 and 3
  2939.  
  2940. ;;
  2941.  
  2942. (defun select-implementation-1-2 (fns snd sel1 sel2 &rest others)
  2943.  
  2944.   (if (numberp sel2)
  2945.  
  2946.     (if (numberp sel1)
  2947.  
  2948.       (apply (aref fns 0) (cons snd (cons sel1 (cons sel2 others))))
  2949.  
  2950.       (apply (aref fns 1) (cons snd (cons sel1 (cons sel2 others)))))
  2951.  
  2952.     (if (numberp sel1)
  2953.  
  2954.       (apply (aref fns 2) (cons snd (cons sel1 (cons sel2 others))))
  2955.  
  2956.       (apply (aref fns 3) (cons snd (cons sel1 (cons sel2 others)))))))
  2957.  
  2958.  
  2959.  
  2960. ;; some waveforms
  2961.  
  2962.  
  2963.  
  2964. (setf *saw-table* (pwlvr -1 1 1))        ; eh, creepy way to get 2205 samples.
  2965.  
  2966. (setf *saw-table* (list *saw-table* (hz-to-step 1) T))
  2967.  
  2968.  
  2969.  
  2970. (setf *tri-table* (pwlvr -1 0.5 1 0.5 -1))
  2971.  
  2972. (setf *tri-table* (list *tri-table* (hz-to-step 1) T))
  2973.  
  2974.  
  2975.  
  2976. (setf *id-shape*  (pwlvr -1 2 1 .01 1))                ; identity
  2977.  
  2978. (setf *step-shape* (seq (const -1) (const 1 1.01)))  ; hard step at zero
  2979.  
  2980.  
  2981.  
  2982. (defun exp-dec (hold halfdec length)
  2983.  
  2984.   (let* ((target (expt 0.5 (/ length halfdec)))
  2985.  
  2986.      (expenv (pwev 1 hold 1 length target)))
  2987.  
  2988.     expenv)
  2989.  
  2990. )
  2991.  
  2992.  
  2993.  
  2994. ;;; operations on sounds
  2995.  
  2996.  
  2997.  
  2998. (defun diff (x y) (sum x (prod -1 y)))
  2999.  
  3000.  
  3001.  
  3002. ; compare-shape is a shape table -- origin 1.
  3003.  
  3004. (defun compare (x y &optional (compare-shape *step-shape*))
  3005.  
  3006.   (let ((xydiff (diff x y)))
  3007.  
  3008.     (shape xydiff compare-shape 1)))
  3009.  
  3010.  
  3011.  
  3012. ;;; oscs
  3013.  
  3014.  
  3015.  
  3016. (defun osc-saw (hz) (hzosc hz *saw-table*))
  3017.  
  3018. (defun osc-tri (hz) (hzosc hz *tri-table*))
  3019.  
  3020.  
  3021.  
  3022. ; bias is [-1, 1] pulse width.  sound or scalar.
  3023.  
  3024. ; hz is a sound or scalar
  3025.  
  3026. (defun osc-pulse (hz bias &optional (compare-shape *step-shape*))
  3027.  
  3028.   (compare bias (osc-tri hz) compare-shape))
  3029.  
  3030.  
  3031.  
  3032.  
  3033.  
  3034.